﻿/*
 * Фильтры изображений
 */

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace DiplomNN
{
    class Filtration
    {
        public static void Processing()
        {
           byte[, ,] SrcImage = Colors.BitmapToByteRgb(new Bitmap(Helper.ImageFromStartFormFiltered));
           SrcImage = Colors.ByteRgbToBlackWhiteByteRgb(SrcImage);
           switch (Helper.ChekButFromStartForm)
           {
               case 1:
                   {
                       Helper.ImageFromStartFormFilteredBinary = Colors.RgbToBitmap(SrcImage);
                       break;
                   }
               case 2:
                   {
                       erosion(ref SrcImage, 1);
                       Helper.ImageFromStartFormFilteredBinary = Colors.RgbToBitmap(SrcImage);
                       break;
                   }
               case 3:
                   {
                       erosion(ref SrcImage, 1);
                       dilation(ref SrcImage, 2);
                       Helper.ImageFromStartFormFilteredBinary = Colors.RgbToBitmap(SrcImage);
                       break;
                   }
           }
        }


        private static void erosion(ref byte[, ,] ImgArr, byte count)
        {
            byte[, ,] ImgArr2 = new byte[ImgArr.GetLength(0), ImgArr.GetLength(1), ImgArr.GetLength(2)];
            for (int n = 0; n < count; n++)
            {
                for (int i = 1; i < ImgArr.GetLength(1) - 1; i++)
                {
                    for (int j = 1; j < ImgArr.GetLength(2) - 1; j++)
                    {
                        int summ = 0;
                        if (ImgArr[0, i - 1, j - 1] < 50) { summ++; }
                        if (ImgArr[0, i - 1, j] < 50) { summ++; }
                        if (ImgArr[0, i - 1, j + 1] < 50) { summ++; }
                        if (ImgArr[0, i, j + 1] < 50) { summ++; }
                        if (ImgArr[0, i + 1, j + 1] < 50) { summ++; }
                        if (ImgArr[0, i + 1, j] < 50) { summ++; }
                        if (ImgArr[0, i + 1, j - 1] < 50) { summ++; }
                        if (ImgArr[0, i, j - 1] < 50) { summ++; }

                        if (summ < 8)
                        {
                            ImgArr2[0, i, j] = 255;
                            ImgArr2[1, i, j] = 255;
                            ImgArr2[2, i, j] = 255;
                        }
                    }
                }
                ImgArr = Colors.BitmapToByteRgb(Colors.RgbToBitmap(ImgArr2));
            }
        }

        private static void dilation(ref byte[, ,] ImgArr, byte count)
        {
            byte[, ,] ImgArr2 = Colors.BitmapToByteRgb(Colors.RgbToBitmap(ImgArr));
            for (int n = 0; n < count; n++)
            {
                for (int i = 1; i < ImgArr.GetLength(1) - 1; i++)
                {
                    for (int j = 1; j < ImgArr.GetLength(2) - 1; j++)
                    {
                        if (ImgArr[0, i, j] < 50)
                        {
                            for (int ii = i - 1; ii < i + 2; ii++)
                            {
                                for (int jj = j - 1; jj < j + 2; jj++)
                                {
                                    ImgArr2[0, ii, jj] = 0;
                                    ImgArr2[1, ii, jj] = 0;
                                    ImgArr2[2, ii, jj] = 0;
                                }
                            }
                        }
                    }
                }
                ImgArr = Colors.BitmapToByteRgb(Colors.RgbToBitmap(ImgArr2));
            }
        }
        
    }
}
